
#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <unistd.h>
using namespace std;

/*
void *MasterCore(void *args)
{
    string threadname = static_cast<const char *>(args);
    while (true)
    {
        cout << threadname << endl;
        sleep(1);
    }
}

void StartMaster(vector<pthread_t> *tidsptr)
{
    pthread_t tid;
    int retval = pthread_create(&tid, nullptr, MasterCore, (void *)"Master Thread");
    if (retval == 0)
    {
        cout << "create master success" << endl;
        tidsptr->push_back(tid);
    }
}

void *SlaverCore(void *args)
{
    string threadname = static_cast<const char *>(args);
    while (true)
    {
        cout << threadname << endl;
        sleep(1);
    }
    
}

void StartSlaver(vector<pthread_t> *tidsptr, int threadnum)
{
    for (int i = 0; i < threadnum; i++)
    {
        char *threadname = new char[64];
        snprintf(threadname, 64, "slaver -%d", i + 1);
        pthread_t tid;
        int retval=pthread_create(&tid,nullptr,SlaverCore,threadname);
        if(retval==0)
        {
            cout<<"create slave success"<<endl;
            tidsptr->push_back(tid);
        }
    }
}

void WaitThread(vector<pthread_t>& tidsptr)
{
    for(auto& tid:tidsptr)
    {
        pthread_join(tid,nullptr);
    }
}

int main()
{
    vector<pthread_t> tids;
    StartMaster(&tids); //
    StartSlaver(&tids, 5);

    WaitThread(tids);
    return 0;
}
*/

pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void* SlaverCore(void* args) //slaver执行任务
{
    string threadname=static_cast<const char*>(args);
    while(true)
    {
        //多个slaver执行任务,保证共享资源的安全性,采用全局互斥锁
        //1.加锁
        pthread_mutex_lock(&mutex);
        //2.保证同步性,各个slaver线程进入等待队列排队,当满足指定的条件变量时,依次执行任务
        pthread_cond_wait(&cond,&mutex);
        cout<<"正在执行任务的线程:"<<threadname<<endl;
        //3.解锁
        pthread_mutex_unlock(&mutex);
    }
}

void* MasterCore(void* args)//master分配任务
{
    sleep(3);
    cout<<"master开始分配任务"<<endl;
    string threadname=static_cast<const char*>(args);
    while(true)
    {
       //唤醒等待队列中首部的线程
       pthread_cond_signal(&cond);
       cout<<"master唤醒一个线程执行任务"<<endl;
       sleep(1);
    }
}

void StartMaster(vector<pthread_t>* tidsptr)
{
    pthread_t tid;
    int retval=pthread_create(&tid,nullptr,MasterCore,(void*)"master thread");
    if(retval==0)
    {
        cout<<"creat master success"<<endl;
        tidsptr->push_back(tid);
    }
    
}

void StartSlaver(vector<pthread_t>* tidsptr,int threadnum)
{
    for(int i=0;i<threadnum;i++)
    {
        char* threadname=new char[64];
        snprintf(threadname,64,"thread -%d\n",i+1);
        pthread_t tid;
        int retval=pthread_create(&tid,nullptr,SlaverCore,threadname);
        if(retval==0)
        {
            cout<<"create slave success"<<endl;
            tidsptr->push_back(tid);
        }

    }
}

void WaitThread(vector<pthread_t>& tids)
{
    for(auto& tid:tids)
    {
        pthread_join(tid,nullptr);
    }
}

int main()
{
    vector<pthread_t> tids;
    StartMaster(&tids);//启动单控制线程
    StartSlaver(&tids,5);//启动多被控制线程
    WaitThread(tids);//回收所有线程

    return 0;
}